package wwk.common.widget;

import java.util.ArrayList;
import java.util.List;
import wwk.common.util.LayoutUtil;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

/**
 * @author wangweike
 * @create_date 2011-12-20����9:51:41
 * @update_date 2011-12-20����9:51:41
 */

public class GroupedListView extends LinearLayout implements OnItemClickListener
{
	private List<Integer> sectionTitleIndexList;//ÿ�εı�������listView�е�������б�
	public ListView listView;
	ListView indexListView;
	int sectionTtitleItemLayoutId;
	int itemLayoutId;
	int itemImageViewId;
	int itemTextViewId;
	Drawable itemImageDrawable;
	int indexItemLayoutId;
	
	public GroupedListView(Context context,List<String> data,int sectionTtitleItemLayoutId,
								int itemLayoutId,
								int itemImageViewId,
								int itemTextViewId,
								Drawable itemImageDrawable,int indexItemLayoutId)
	{
		super(context);
		LinearLayout.LayoutParams layoutParams=LayoutUtil.getLinearLayoutLayoutParamsFillParent();
		this.setLayoutParams(layoutParams);
		this.setOrientation(LinearLayout.HORIZONTAL);
		
		this.sectionTtitleItemLayoutId=sectionTtitleItemLayoutId;
		this.itemLayoutId=itemLayoutId;
		this.itemImageViewId=itemImageViewId;
		this.itemTextViewId=itemTextViewId;
		this.itemImageDrawable=itemImageDrawable;
		this.indexItemLayoutId=indexItemLayoutId;
	}
	
	public GroupedListView(Context context, AttributeSet attrs)
	{
        super(context, attrs);
    }
	
	public void setListData(List<String> data)
	{
		Context context=getContext();
		
		 List<String> indexTextList= new ArrayList<String>();//ÿ�εı������ֵ��б�
		 sectionTitleIndexList = new ArrayList<Integer>();//ÿ�εı�������listView�е�������б�
		 GroupedListViewAdapter.getSectionedListData(data,indexTextList, sectionTitleIndexList);
		 
		//listView
		 if(listView==null)
		 {
			 listView = new ListView(context);
		 
			 LinearLayout.LayoutParams layoutParams=LayoutUtil.getLinearLayoutParamsWrapContentFillParent();
			 layoutParams.weight=0.1f;
			 listView.setLayoutParams(layoutParams);
			 this.addView(listView);
		 }

		GroupedListViewAdapter listAdapter= new GroupedListViewAdapter(context,data, 
				sectionTitleIndexList,
				sectionTtitleItemLayoutId,itemLayoutId,
				itemImageViewId,itemTextViewId,itemImageDrawable);
		listView.setAdapter(listAdapter);
	
		//indexListView
		 if(indexListView==null)
		 {
			 indexListView =new ListView(context);
			LinearLayout.LayoutParams layoutParams=LayoutUtil.getLinearLayoutParamsWrapContentFillParent();
			layoutParams.weight=0.9f;
			indexListView.setLayoutParams(layoutParams);
			indexListView.setDividerHeight(0);
			indexListView.setVerticalScrollBarEnabled(false);
			indexListView.setOnItemClickListener(this);
			this.addView(indexListView);
		 }
		 
		indexListView.setAdapter(new ArrayAdapter<String>(context,indexItemLayoutId, indexTextList));
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id)
	{
		Log.d("position",position+"      "+sectionTitleIndexList.size());
		//listView.setSelectionFromTop(sectionTitleIndexList.get(position), 0);
	}
}
